package cn.edu.ccut.refactor;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

/**
 * 
 * @author Bai Yucheng 2008-1-10
 * 
 */
public class RenameSaturnModuleProcessor extends AbstractRenameProcessor {
	private IJavaElement element;
	private String fNewElementName;

	public RenameSaturnModuleProcessor(IJavaElement ele) {
		element = ele;
		if (element != null)
			setNewElementName(element.getElementName());
	}

//	private static IFolder getFolder(IPath ipath) {
//		return ResourcesPlugin.getWorkspace().getRoot().getFolder(ipath);
//	}

	public void setNewElementName(String s) {
		Assert.isNotNull(s);
		fNewElementName = s;
	}

	public String getNewElementName() {
		return fNewElementName;
	}

	public String getCurrentElementName() {
		return element.getElementName();
	}

	public Object getNewElement() throws CoreException {
		return ResourcesPlugin.getWorkspace().getRoot().findMember(
				createNewPath(getNewElementName()));
	}

	public RefactoringStatus checkNewElementName(String s) throws CoreException {
		Assert.isNotNull(s, "new name"); //$NON-NLS-1$
		IContainer icontainer = element.getResource().getParent();
		if (s.equals(getCurrentElementName()))
			return RefactoringStatus.createFatalErrorStatus(Messages.getString("RenameSaturnModuleProcessor.1")); //$NON-NLS-1$
		if (icontainer == null)
			return RefactoringStatus.createFatalErrorStatus(Messages.getString("RenameSaturnModuleProcessor.2")); //$NON-NLS-1$
		if (icontainer.findMember(s) != null)
			return RefactoringStatus.createFatalErrorStatus(Messages.getString("RenameSaturnModuleProcessor.3")); //$NON-NLS-1$
		if (!icontainer.getFullPath().isValidSegment(s))
			return RefactoringStatus.createFatalErrorStatus(Messages.getString("RenameSaturnModuleProcessor.4")); //$NON-NLS-1$
        RefactoringStatus refactoringstatus = RefactoringStatus.create(icontainer.getWorkspace().validateName(s, element.getElementType()));
//        if(!refactoringstatus.hasFatalError())
//            refactoringstatus.merge(RefactoringStatus.create(icontainer.getWorkspace().validatePath(createNewPath(s), element.getElementType())));
        return refactoringstatus;
	}

	public Change createChange(IProgressMonitor iprogressmonitor)
			throws CoreException, OperationCanceledException {
		iprogressmonitor.beginTask("", 1); //$NON-NLS-1$
		CompositeChange compositechange1;
		try {
			String s = element.getElementName();
//			String s = getSimpleName(saturnFile.getName());
//			String s1 = MessageFormat.format(Messages.getString("RenameSaturnFileProcessor.7"), new String[] { s });
			String s1 = Messages.getString("RenameSaturnModuleProcessor.6"); //$NON-NLS-1$
			CompositeChange compositechange = new CompositeChange(s1);
//			if (isUpdateReference()) {
				String key = null;
				String newKey = null;
				int flag = 0;
  	
//    			int i = key.indexOf(s);
//				String s4 = key.substring(0, i);
//				newKey = s4 + getNewElementName();
				
				key = getKey(s);
				newKey = getKey(getNewElementName());
				
				IResource ires = element.getParent().getResource();
				SearchEngine searchengine = new SearchEngine(ires, key, newKey, flag);
				Change achange[] = searchengine.search(iprogressmonitor);
				compositechange.addAll(achange);
//			}

			compositechange.add(new RenameSaturnModuleChange(element, getNewElementName()));
	
			compositechange1 = compositechange;
		} finally {
			iprogressmonitor.done();
		}
		return compositechange1;
	}

	private String getKey(String str) {
		if (str.indexOf(".") != -1) { //$NON-NLS-1$
			str = str.substring(str.lastIndexOf(".") + 1); //$NON-NLS-1$
		}
		return str;
	}
	
	private String createNewPath(String s) {
		return element.getResource().getFullPath().removeLastSegments(1).append(s).toString();
	}

	public Object[] getElements() {
		return (new Object[] { element });
	}

	public IJavaElement getElement() {
		return element;
	}

	public void setElement(IJavaElement element) {
		this.element = element;
	}


}